/*
******************
* author: 马丰彦
* date: 2019-05-08
* function: 用户登录登出、存取cookie、以及获取权限
******************
*/

import { login, logout } from '@/api/user'
import { Message } from 'element-ui'
import { getUserPower } from '@/api/userPower.js'
import { getToken, setToken, removeToken } from '@/utils/auth'
// import { resetRouter } from '@/router'
import { showPower } from '@/utils/power.js'

const state = {
  token: getToken(),
  name: '',
  avatar: '',
  system: [],
  roles: [],
  plat: [],
  assignment: [],
  busSystem: []
}

const mutations = {
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  },
  SET_ROLES: (state, roles) => {
    state.roles = roles
  },
  SET_PLAT: (state, plat) => {
    state.plat = plat
  },
  SET_ASSIGNMENT: (state, assignment) => {
    state.assignment = assignment
  },
  SET_BUSSYSTEM: (state, busSystem) => {
    state.busSystem = busSystem
  },
  SET_SYSTEM: (state, system) => {
    state.system = system
  }
}

const actions = {
  // 用户登录 action
  Login({ commit }, param) {
    return new Promise((resolve, reject) => {
      login(param).then(response => {
        // 如果token为空值，拒绝访问
        // console.log(response.data.userId)
        // setUser(response.data.userId)
        // setRole(response.data.roleName)
        const token = response.data.token
        // console.log(response.token)
        if (!token) {
          Message({
            message: '拒绝访问',
            type: 'error'
          })
          reject()
        } else {
          commit('SET_TOKEN', token)
          // console.log(token)
          setToken(token)
          // console.log(getToken())
          // setUserId(response.userName)

          // setUserAvatar(data.avatar)
          // setPlatformKey(data.platform)
          // setRoleKey(data.role)
          resolve()
        }
      }).catch(error => {
        reject(error)
      })
    })
  },
  // 得到用户权限
  GetInfo({ commit }) {
    return new Promise((resolve, reject) => {
      getUserPower().then(response => {
        if (response.resultCode !== '0000') {
          reject(response.resultDesc)
          return false
        }
        // 为了安全性，此处不存入token
        const roles = response.data.sysList
        commit('SET_ROLES', roles)
        commit('SET_SYSTEM', showPower(roles))
        resolve(roles)
      }).catch((err) => {
        console.log(err)
      })
    })
  },

  // 登出
  LogOut({ commit, state }) {
    return new Promise((resolve, reject) => {
      logout(state.token).then(() => {
        commit('SET_TOKEN', '')
        commit('SET_ROLES', [])
        removeToken()
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // remove token
  resetToken({ commit }) {
    return new Promise(resolve => {
      commit('SET_TOKEN', '')
      commit('SET_ROLES', [])
      removeToken()
      resolve()
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

